import datetime
from flask import Flask, render_template, request, redirect
from jinja2 import Markup

from company import company_info, company_table,  company_start, company_top, company_startRate
from overview import start_rate, workTime_top, workTime_count
from equipments import   equipment_state, equipment_startrate, equipments_statetop

app = Flask(__name__)
app.config['SECRET_KEY'] = 'LYX'

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'GET':
        return render_template('overview.html')
    if request.method == 'POST':
        return redirect('Company')

@app.route('/Company', methods=['GET', 'POST'])
def Company():
    if request.method == 'GET':
        return render_template('company.html')
    if request.method == 'POST':
        return redirect('Equipment')

@app.route('/Equipment', methods=['GET','POST'])
def Equipment():
    if request.method == 'GET':
        ename = request.args.get('ename', True)
    if request.method == 'POST':
        ename = request.form.get('ename')
    print("设备名称",ename)
    res = {}
    # res = rank_list(ename)
    res['equipMentNumber'] = ename
    return render_template('equipment.html',data=res)


@app.route('/CompanyInfos')
def CompanyInfos():
    cname = request.args.get("cname", True)
    print('公司名称',cname)
    date = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime('%Y-%m-%d')
    cinfos = company_info(cname,date)
    table = company_table(cinfos,cname,date)
    return Markup(table.render_embed())


@app.route('/CompanyStartRate')
def CompanyStartRate():
    cname = request.args.get("cname", True)
    date = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime('%Y-%m-%d')
    cinfos = company_info(cname,date)
    line = company_startRate(cinfos)
    return Markup(line.render_embed())


@app.route('/CompanyTop')
def CompanyTop():
    cname = request.args.get("cname", True)
    date = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime('%Y-%m-%d')
    cinfos = company_info(cname,date)
    cstart = company_start(cinfos)
    bar = company_top(cstart)
    return Markup(bar.render_embed())

@app.route('/EquipmentState')
def EquipmentState():
    equipMentNumber = request.args.get("ename", True)
    liquid = equipment_state(equipMentNumber)
    return Markup(liquid.render_embed())

@app.route('/EquipmentStartRate')
def EquipmentStartRate():
    equipMentNumber = request.args.get("ename", True)
    line = equipment_startrate(equipMentNumber)
    return Markup(line.render_embed())

@app.route('/EquipmentsStateTop')
def EquipmentsStateTop():
    equipMentNumber = request.args.get("ename", True)
    bar = equipments_statetop(equipMentNumber)
    return Markup(bar.render_embed())



@app.route('/StartRate')
def StartRate():
    line = start_rate()
    return Markup(line.render_embed())

@app.route('/WorkTimeTop')
def WorkTimeTop():
    bar = workTime_top()
    return Markup(bar.render_embed())

@app.route('/WorkTimeCount')
def WorkTimeCount():
    bar = workTime_count()
    return Markup(bar.render_embed())

# 启动flask
if __name__ == '__main__':
    app.run(host='0.0.0.0',port=5000,debug=True)